//AC
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 10;

int version[N]; // version[0]是服务器,记录字符串的版本号

pair<int, string> record[N];
// first : father
//  second :  追加的字符

int main() {

  int n, q, idx = 0;
  cin >> n >> q;

  for (int i = 1; i <= q; i++) {
    int op, p;
    cin >> op >> p;
    if (op == 1)
      version[p] = version[0];
    else if (op == 2) {
      string s;
      cin >> s;
      record[++idx] = {version[p], s};
      version[p] = idx;
    } else
      version[0] = version[p];
  }

  vector<char> buf;
  int v = version[0];// 服务器的版本号
  while (v) {  // v不为0时，继续向上查找父节点
    auto s = record[v].second;
    reverse(s.begin(), s.end());
    for (auto x : s)
      buf.push_back(x);
    v = record[v].first; // 继续向上查找父节点
  }

  reverse(buf.begin(), buf.end());
  for (auto c : buf) {
    cout << c;
  }

  return 0;
}